package com.example.smartcanteen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.smartcanteen.mapper.StudentMapper;
import com.example.smartcanteen.model.Student;
import com.example.smartcanteen.utils.JwtUtil;
import com.example.smartcanteen.utils.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;


    //学生登录功能
    public ResponseMessage login(Student student) {
        ResponseMessage responseMessage = new ResponseMessage();

        //根据学生账号、密码验证登录
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", student.getUsername())
                .eq("password", student.getPassword());
        Boolean success = studentMapper.selectOne(queryWrapper) != null;
        responseMessage.setData(studentMapper.selectList(queryWrapper));
        System.out.println(responseMessage.getData());

        //生成token
//        Map<String, Object> claims = new HashMap<>();
//        String token = JwtUtil.genToken(claims);
//        responseMessage.setToken(token);

        responseMessage.setSuccess(success);
        return responseMessage;
    }


    //通过调用学生登录功能实现学生修改密码功能
    public ResponseMessage update(Student student, String newpassword) {
        ResponseMessage responseMessage = new ResponseMessage();
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();

        //先验证账号、密码是否正确、再修改新密码
        Boolean success = login(student).getSuccess();
        if (success) {
            updateWrapper.set("password", newpassword);
            studentMapper.update(updateWrapper);
        }
        responseMessage.setSuccess(success);
        return responseMessage;
    }
}
